package com.heima.article.controller.v1;

import com.heima.api.behavior.IBehaviorClient;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.WmApAuthorService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ApArticleDto;
import com.heima.model.article.dtos.ApArticleHomeDto;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.behavior.dtos.ArticleInfoDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.utils.common.WmThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2023/3/14 9:47
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1/article")
@Slf4j
public class ApArticleController{
    @Autowired
    private IBehaviorClient behaviorClient;
    @Autowired
    private ApArticleService articleService;
    @Autowired
    private WmApAuthorService wmApAuthorService;
    @PostMapping("/load")
    public ResponseResult load(@RequestBody ApArticleHomeDto homeDto){
        return articleService.load(homeDto, ArticleConstants.LOAD_MORE);
    }

    @PostMapping("/loadmore")
    public ResponseResult loadMore(@RequestBody ApArticleHomeDto homeDto){
        return articleService.load(homeDto,ArticleConstants.LOAD_MORE);
    }

    @PostMapping("/loadnew")
    public ResponseResult loadNew(@RequestBody ApArticleHomeDto homeDto){
        return articleService.load(homeDto,ArticleConstants.LOAD_NEW);
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody ApArticleDto articleDto){
        return articleService.saveArticle(articleDto);
    }

    @PostMapping("/ap_author/{id}")
    public ApAuthor getById(@PathVariable int id){
        return wmApAuthorService.getArticleName(id);
    }


    @PostMapping("/load_article_behavior")
    public ResponseResult loadArticleBehavior(@RequestBody ArticleInfoDto dto){
        dto.setUserId(WmThreadLocalUtil.getUser().getId());
        log.info("加载文章的点赞、收藏、评论数据{}",dto);
        return behaviorClient.loadArticleBehavior(dto);
    }
}